package root.electronclass.electronclass.school

import android.view.LayoutInflater
import android.widget.LinearLayout
import android.widget.PopupWindow
import androidx.activity.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import root.electronclass.electronclass.common.base.BasicAdapter
import root.electronclass.electronclass.common.base.ViewBindingActivity
import root.electronclass.electronclass.R
import root.electronclass.electronclass.databinding.ActivitySchoolHonorBinding
import root.electronclass.electronclass.databinding.ItemPopupwindowBinding
import root.electronclass.electronclass.databinding.SchoolHonorItemBinding
import root.electronclass.electronclass.databinding.SchoolHonorItemItemBinding
import root.electronclass.electronclass.school.model.SchoolNotificationModel
import root.electronclass.electronclass.setup
import root.electronclass.electronclass.mvp.entity.HonorItem
import root.electronclass.electronclass.mvp.entity.HonorList

/**
 * 校园荣誉墙
 */
class SchoolHonorActivity : ViewBindingActivity<ActivitySchoolHonorBinding>() {
    private val viewModel: SchoolNotificationModel by viewModels()
    lateinit var popupWindow: PopupWindow
    private var subAdapter: BasicAdapter<String, ItemPopupwindowBinding>? = null
    override fun init() {
        binding.top.setup(lifecycle, "校园荣誉墙")
        showPopWindow()
        binding.recyclerView2.adapter = adapter
        binding.back.back.setOnClickListener {
            finish()
        }
        binding.subjectCl.setOnClickListener {
            popupWindow.showAsDropDown(binding.subjectCl)
        }
        binding.subjectTv.text = "区"
        viewModel.rcSHonor.observe(this){ its->
            adapter.replaceAll(its)
        }
        viewModel.getRcHonorList(1)
    }
    private val adapter = BasicAdapter<HonorList, SchoolHonorItemBinding> { holder, item ->
        holder.binding.itemTitle.text = item.year+"年"
        val childAdapter = BasicAdapter<HonorItem, SchoolHonorItemItemBinding> { holder1, childItem ->
            holder1.binding.picSdw.setImageURI(if (childItem.imgUrlFormat.isNotEmpty()) childItem.imgUrlFormat[0].url else "")
            holder1.binding.tvType.text = when(childItem.prizeType){
                1 -> "区级"
                2 -> "市级"
                else -> "省级"
            }
            holder1.binding.time.text = childItem.createTime
            holder1.binding.title.text = childItem.name
        }
        holder.binding.itemRecycler.layoutManager = GridLayoutManager(this@SchoolHonorActivity, 2)
        holder.binding.itemRecycler.adapter = childAdapter
        childAdapter.refresh(item.honorList)
    }




   private fun showPopWindow() {
        //自定义布局，作为PopupWindow显示的内容
        val contentView = LayoutInflater.from(this).inflate(R.layout.pop_window, null);
        popupWindow = PopupWindow(contentView, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true)
        val recyclerView = contentView.findViewById<RecyclerView>(R.id.recycler)
        subAdapter = BasicAdapter { holder, item ->
            holder.binding.tv.text = item
            holder.binding.tv.setOnClickListener {
                binding.subjectTv.text = item
                viewModel.getRcHonorList(when(item){
                    "区" -> 1
                    "市" -> 2
                    else -> 3
                })
                popupWindow.dismiss()
            }
        }
        recyclerView.adapter = subAdapter
        subAdapter?.refresh(arrayListOf("区","市","省"))
        popupWindow.isTouchable = true
        popupWindow.setTouchInterceptor { p0, p1 -> false };
    }

}